package com.admin.service.impl;

import com.admin.entity.StationRecharge;
import com.admin.mapper.StationRechargeMapper;
import com.admin.service.IStationRechargeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author marisheng
 * @since 2019-11-21
 */
@SuppressWarnings("ALL")
@Service
public class StationRechargeServiceImpl extends ServiceImpl<StationRechargeMapper, StationRecharge> implements IStationRechargeService {
    @Autowired
    private StationRechargeMapper rechargeMapper;

    @Autowired
    private AdminServiceImpl adminService;
    @Autowired
    private StationServiceImpl stationService;

    /**
     * 往充值火币的交易记录表新增数据以及修改站点的最大终端数量和用户的火币数量
     *
     * @param sr
     * @return
     */
    @Override
    public int rechargeById(StationRecharge sr) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime time = LocalDateTime.now();
        String localTime = df.format(time);
        LocalDateTime ldt = LocalDateTime.parse(localTime,df);
        sr.setStationRechargeTime(ldt);
        int n1=adminService.editMoney(sr);//查找用户的火币并且修改
        if (n1== -1) {
            return 400;
        }
        int n3 = stationService.editterminals(sr);//修改站点的终端数量
        int n2= rechargeMapper.insert(sr);//向站点火币交易表插入数据
        return 200;
    }

    /**
     * 查询所有的站点交易记录
     * @param page
     * @param limit
     * @param map
     * @return
     */
    @Override
    public IPage selectStationRecharge(int page, int limit, Map map) {
        Page p = new Page(page, limit);
        return rechargeMapper.selectRecharge(p, map);
    }
}
